//
//  CustomButton.m
//  CommonUI
//
//  Created by xiong qi on 13-1-5.
//  Copyright (c) 2013年 appfactory. All rights reserved.
//

#import "CustomButton.h"

@implementation CustomButton
@synthesize BackGroundNormalImagePath,BackGroundHighLightImagePath,NormalImagePath,HighLightImagePath,isSelect,groupID;
@synthesize titleLabel,NormalTitleColor,HighLightTitleColor;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.exclusiveTouch = YES;
        
        backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:backgroundImageView];
        [backgroundImageView release];
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [backgroundImageView addSubview:imageView];
        [imageView release];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20, frame.size.height-10)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont fontWithName:@"Arial" size:13];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [self addSubview:titleLabel];
        [titleLabel release];
        
    }
    return self;
}

-(void)dealloc
{
    self.NormalImagePath = nil;
    self.HighLightImagePath = nil;
    self.BackGroundHighLightImagePath = nil;
    self.BackGroundNormalImagePath = nil;
    self.NormalTitleColor = nil;
    self.HighLightTitleColor = nil;
    [super dealloc];
}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
	BOOL flag =[super beginTrackingWithTouch:touch withEvent:event] ;
	
	[self setHighLight];
    TouchFlag = YES;
	
	return flag;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
	[super endTrackingWithTouch:touch withEvent:event];
    
    if (TouchFlag) {
        
        CGPoint point = [touch locationInView:self];
        
        if (!CGRectContainsPoint(self.bounds,point)) {
            if (!self.isSelect) {
                [self setNormal];
            }
            return;
        }
        [self setOnlyHigh];
        
        TouchFlag = NO;
    }
    
	
}

- (void)cancelTrackingWithEvent:(UIEvent *)event
{
	[super cancelTrackingWithEvent:event];
    
    //需要高亮的按钮，选中状态下是不需要变成Normal颜色的
    if (!self.isSelect) {
        [self setNormal];
    }
}

-(void)setIsSelect:(BOOL)value
{
    if (value) {
        [self setHighLight];
    }
    else
    {
        [self setNormal];
    }
    isSelect = value;
}

-(void)setOnlyHigh
{
    //有分组的按钮
    if (self.groupID != 0) {
        //现阶段只考虑按钮都在同一个超类，如有需要可以再行修改
        for (CustomButton * item in [self superview].subviews) {
            if ([item isKindOfClass:[CustomButton class]] && item.groupID>0 &&item.groupID == self.groupID && item != self) {
                item.isSelect = NO;
            }
            else if(item == self)
            {
                //本按钮被点击
                item.isSelect = YES;
            }
        }
    }
    else
    {
        if (isSelect) {
            self.isSelect = NO;
        }
        else
        {
            self.isSelect = YES;
        }
    }
}

-(void)setNormal
{
    imageView.image = [UIImage imageNamed:NormalImagePath];
    backgroundImageView.image = [UIImage imageNamed:BackGroundNormalImagePath];
    titleLabel.textColor = self.NormalTitleColor;
}

-(void)setHighLight
{
    imageView.image = [UIImage imageNamed:HighLightImagePath];
    backgroundImageView.image = [UIImage imageNamed:BackGroundHighLightImagePath];
    titleLabel.textColor = self.HighLightTitleColor;
}
@end
